//
//  UIScrollView+DF.m
//  换换圈
//
//  Created by 艾柯斯 on 14-12-1.
//  Copyright (c) 2014年 艾柯斯. All rights reserved.
//

#import "UIScrollView+DF.h"


@implementation UIScrollView (DF)


-(void)scrollsToBottom {
    [self scrollsToBottomWithAnimated:YES];
}


-(void)scrollsToBottomWithAnimated:(BOOL)animated {
    if(self.bounds.size.height < self.contentSize.height) {
        
        [self setContentOffset:CGPointMake(0, self.contentSize.height - self.bounds.size.height) animated:animated];
    }
}





-(void)scrollsToKeyboardWithNotification:(NSNotification *)notification animated:(BOOL)animated {
    [self scrollsToKeyboardWithNotification:notification animated:nil topSpacing:0 bottomSpacing:0];

}


-(void)scrollsToKeyboardWithNotification:(NSNotification *)notification animated:(BOOL)animated topSpacing:(CGFloat)topSpacing bottomSpacing:(CGFloat)bottomSpacing {
    NSDictionary *userInfo = [notification userInfo];
    
    // get keyboard rect in windwo coordinate
    CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // convert keyboard rect from window coordinate to scroll view coordinate
    keyboardRect = [self convertRect:keyboardRect fromView:nil];
    
    // get first responder textfield
    UIView *currentResponder = [self findFirstResponderBeneathView:self];
    if (currentResponder != nil) {
        // convert textfield left bottom point to scroll view coordinate
        
        CGRect firstResponderFrame = [currentResponder convertRect:(CGRect){CGPointZero,currentResponder.frame.size} toView:self];
        CGFloat currentOffsetY =  self.contentOffset.y;
        CGFloat offsetY = firstResponderFrame.origin.y - topSpacing;
        if(currentOffsetY > offsetY) {
            [self setContentOffset:CGPointMake(self.contentOffset.x, offsetY) animated:animated];
        }else {
            offsetY = firstResponderFrame.origin.y + firstResponderFrame.size.height - keyboardRect.origin.y + bottomSpacing;
            if (offsetY > 0) {
                [self setContentOffset:CGPointMake(self.contentOffset.x, self.contentOffset.y + offsetY) animated:animated];
            }
        }
        
    }
}



//
//-(void)scrollsToKeyboardWithNotification:(NSNotification *)notification subview:(UIView *)subview {
//    
//    
//    CGRect frame = [subview convertRect:subview.bounds toView:[[UIApplication sharedApplication] keyWindow]];
//
////    CGRect frame = subview.frame;
////    UIView* superview = subview;
////    while (superview != self && superview.superview) {
////        superview = superview.superview;
////        MyLog(@"%@",[NSValue valueWithCGRect:frame]);
////        frame = [subview convertRect:frame toView:superview];
////    }
////    if(self != superview) {
////        return;
////    }
//    NSDictionary *userInfo = [notification userInfo];
//
//    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    //keyboardFrame = [subview convertRect:keyboardFrame toView:[[UIApplication sharedApplication] keyWindow]];
//
//    
//    if(frame.origin.y + frame.size.height > keyboardFrame.origin.y) {
//        CGPoint contentOffset = self.contentOffset;
//        MyLog(@"修改前 --- %@",[NSValue valueWithCGPoint:contentOffset]);
//        contentOffset.y = contentOffset.y - (frame.origin.y + frame.size.height - keyboardFrame.origin.y) - 5;
//        MyLog(@"修改后 --- %@",[NSValue valueWithCGPoint:contentOffset]);
//        [self setContentOffset:contentOffset animated:YES];
//    }
//    CGFloat adjustHeight = originalContentViewFrame.origin.y + originalContentViewFrame.size.height;
//    
//    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,keyboardFrame.size.height, 0.0);
//    self.contentInset = contentInsets;
//
//    
//}

- (UIView *)findFirstResponderBeneathView:(UIView *)view {
    // 递归查找第一响应者
    for (UIView *childView in view.subviews ) {
        if ([childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] ) {
            return childView;
        }
        UIView *result = [self findFirstResponderBeneathView:childView];
        if (result) {
            return result;
        }
    }
    return nil;
}


@end
